package com.example.administrator.wallet.view.activity.find.cointrans;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.administrator.wallet.R;
import com.example.administrator.wallet.adapter.MarketTransactionRecordsAdapter;
import com.example.administrator.wallet.base.BaseActivity;
import com.example.administrator.wallet.constant.Constant;
import com.example.administrator.wallet.custom.MessageEvent;
import com.example.administrator.wallet.custom.MyWebSocketService;
import com.example.administrator.wallet.model.bean.WebSocketOrderHistoryBean;
import com.example.administrator.wallet.utils.SharedPrefsUtil;
import com.google.gson.Gson;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

public class MarketTransactionRecodsActivity extends BaseActivity {
    @Bind(R.id.finish_img)
    ImageButton finishImg;
    @Bind(R.id.view_title)
    TextView viewTitle;

    @Bind(R.id.listView_market_transaction_activity)
    ListView listView;
    private WebSocketOrderHistoryBean webSocketOrderHistoryBean;
    private MarketTransactionRecordsAdapter adapter;
    private List<WebSocketOrderHistoryBean.ResultBean.RecordsBean> list;
    private Gson mGson;
    private String token;
    private int joinId;
    private String market;
    private int orderId;

    private MyWebSocketService myWebSocketService;
    private ServiceConnection serviceConnection;

    @Override
    public void init() {
        EventBus.getDefault().register(this);
        initView();
        initData();
    }

    private void initView() {
        finishImg.setVisibility(View.VISIBLE);
        Intent intent = getIntent();
        String title = intent.getStringExtra(Constant.TO_MARKET_TRANSACTION_RECORDS_ACTIVITY);
        viewTitle.setText(title + getResources().getString(R.string.Transaction_Records));

        String[] markets = title.split("/");
        market = markets[0] + markets[1];
    }

    private void initData() {
        list = new ArrayList<>();
        adapter = new MarketTransactionRecordsAdapter(this,list);
        listView.setAdapter(adapter);

        mGson = new Gson();
        token = SharedPrefsUtil.getValue(this, "wallet", "token", "");

        serviceConnection = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                myWebSocketService = ((MyWebSocketService.ServiceBinder) service).getService();
                JSONArray jsonArray = new JSONArray();
                jsonArray.add(market);
                jsonArray.add(0);
                jsonArray.add(0);
                jsonArray.add(0);
                jsonArray.add(10);
                orderId = myWebSocketService.sendMessage("order.history",jsonArray);
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {

            }
        };

        bindService(new Intent(this,MyWebSocketService.class),serviceConnection, Context.BIND_AUTO_CREATE);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void Event(MessageEvent event){
        JSONObject jsonObject = (JSONObject) JSONObject.parse(event.getMessage());
        if (jsonObject.getInteger("id") == orderId){

            webSocketOrderHistoryBean = mGson.fromJson(event.getMessage(),WebSocketOrderHistoryBean.class);
            list.clear();
            list.addAll(webSocketOrderHistoryBean.getResult().getRecords());

            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public int getLayoutResource() {
        return R.layout.activity_market_transaction_recods;
    }

    @OnClick(R.id.finish_img)
    public void onViewClicked(View view) {
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }
}
